function assignCustomItemsCrowd()
--------------------------------------------------------------------------------------------------------------
--PLACE CUSTOM ASSIGNMENTS BELOW HERE
--------------------------------------------------------------------------------------------------------------




--------------------------------------------------------------------------------------------------------------
--PLACE CUSTOM ASSIGNMENTS ABOVE HERE
--------------------------------------------------------------------------------------------------------------
end

--------------------------------------------------------------------------------------------------------------

function CrowdUpdate()
	local as = gSportsRNA
	local state = as:GetTable("wvState")
	local crowd = as:GetTable("wvCrowd")
		
	db.crowd.lightID = as:GetInt(crowd, "stadLightID")
	db.crowd.homeTeamID = as:GetInt(crowd, "homeTeamID")
	db.crowd.awayTeamID = as:GetInt(crowd, "awayTeamID")
	db.crowd.homeKitTeamID = as:GetInt(crowd, "homeKitTeamID")
	db.crowd.homeKitTypeID = as:GetInt(crowd, "homeKitTypeID")
	db.crowd.awayKitTeamID = as:GetInt(crowd, "awayKitTeamID")
	db.crowd.awayKitTypeID = as:GetInt(crowd, "awayKitTypeID")

	db.crowd.wet = math.min( 1, as:GetInt(state, "wvAttribStadWeather" ))
	
	assignCustomItemsCrowd()
	
	db.crowd.homecrowdsize = 4 - getHomeCrowdSize(db.crowd.homeTeamID,4)
	db.crowd.awaycrowdsize = 4 - getHomeCrowdSize(db.crowd.awayKitTeamID,4)
	db.crowd.neutralcrowdsize = db.crowd.homecrowdsize

end

function CrowdAssetBind(crowd)
	local gr = gRenderables
   	local lod = 0
	local priority = 1
	
	gr:AddCallback(crowd, lod, "CrowdUpdate()")
	
	gr:AddAsset(crowd, lod, "shader", "data/fifarna/shader.big", priority)
	
	-- TODO: enable weather crowd. Value should be 0 or 1 (last field before .rx3)
	
	assignCustomItemsCrowd()
	
	homecrowdsize = 4 - homecrowdsize
	awaycrowdsize = 4 - awaycrowdsize
	neutralcrowdsize = homecrowdsize
	
	-- Characters per group
	local characters = 4
	local groupcount = 3
	
	gr:AddAsset(crowd, lod, "crowd_common_textures", "data/sceneassets/crowd/crowd_common_textures.rx3", priority)
	
	for i=0,groupcount-1 do
		for j=0,characters-1 do
		
		if ( ((i == 0) and (j >= neutralcrowdsize)) or ((i == 1) and (j >= homecrowdsize)) or ((i == 2) and (j >= awaycrowdsize)) ) then
		
			local meshpart = "bodymesh_"..i.."_"..j
			local texpart = "bodytex_"..i.."_"..j
			-- if it's the 4th mesh in an away or neutral zone, create a jersey crowd character
			-- i == 0 -> neutral section
			-- j == 3 -> 4th mesh
			if (j == 3 and i > 0) then
				-- create mesh for body without jersey
				local bodymesh = meshpart.."_0"
				gr:AddAsset(crowd, lod, bodymesh, "data/sceneassets/crowd/crowd_0_4_${db.crowd.lightID}_${db.crowd.wet}.rx3", priority)
				gr:AddAsset(crowd, lod, texpart, "data/sceneassets/crowd/crowd_0_4_${db.crowd.lightID}_${db.crowd.wet}_textures.rx3", priority)
				
				gr:CreateMaterial(crowd, lod, bodymesh, "crowd_body.fx")
				gr:SetSubMesh(crowd, lod, bodymesh, "crowd_")
				gr:SetTexture(crowd, lod, bodymesh, "textures", "diffuseTexture", texpart, "") -- grabs first texture from the texlib
				
				-- create mesh for jersey
				local jerseymesh = meshpart.."_1"
				gr:AddAsset(crowd, lod, jerseymesh, "data/sceneassets/crowd/crowd_0_4_${db.crowd.lightID}_${db.crowd.wet}.rx3", priority)
				
				gr:CreateMaterial(crowd, lod, jerseymesh, "crowd_jersey.fx")
				gr:SetSubMesh(crowd, lod, jerseymesh, "crowdjersey_")
				if i == 1 then
					-- home group
					gr:SetTextureFromRuntime(crowd, lod, jerseymesh, "textures", "diffuseTexture", "jersey_${db.crowd.homeKitTeamID}_${db.crowd.homeKitTypeID}_0_0")
				else
					-- away group
					gr:SetTextureFromRuntime(crowd, lod, jerseymesh, "textures", "diffuseTexture", "jersey_${db.crowd.awayKitTeamID}_${db.crowd.awayKitTypeID}_0_1")
				end
				gr:SetTexture(crowd, lod, jerseymesh, "textures", "detailTexture", "crowd_common_textures", "crowdjersey_am")
			else
				gr:AddAsset(crowd, lod, meshpart, "data/sceneassets/crowd/crowd_0_"..j.."_${db.crowd.lightID}_${db.crowd.wet}.rx3", priority)
				gr:AddAsset(crowd, lod, texpart, "data/sceneassets/crowd/crowd_0_"..j.."_${db.crowd.lightID}_${db.crowd.wet}_textures.rx3", priority)
				
				gr:CreateMaterial(crowd, lod, meshpart, "crowd_body.fx")
				gr:SetTexture(crowd, lod, meshpart, "textures", "diffuseTexture", texpart, "") -- grabs first texture from the texlib
			end
			
			end
			
		end
	end
	
	
	-- Chairs
	if (seatsvisable) then
	gr:AddAsset(crowd, lod, "chairmesh_0", "data/sceneassets/crowdchair/crowdchair_0.rx3", priority)
	gr:AddAsset(crowd, lod, "chairtex_0", "data/sceneassets/crowdchair/chaircmn_textures.rx3", priority)
	gr:CreateMaterial(crowd, lod, "chairmesh_0", "crowd_chair.fx")
	gr:SetTexture(crowd, lod, "chairmesh_0", "textures", "diffuseTexture", "chairtex_0", "")
	end

	-- Scarves
	local scarfcount = 4
	for i=0,groupcount-1 do
		for j=0,scarfcount-1 do
		
		if ( ((i == 0) and (j >= neutralcrowdsize)) or ((i == 1) and (j >= homecrowdsize)) or ((i == 2) and (j >= awaycrowdsize)) ) then
		
			local meshpart = "scarfmesh_"..i.."_"..j
			local texpart = "scarftex_"..i.."_"..j
			local flagindex = math.fmod(j, 2)
			gr:AddAsset(crowd, lod, meshpart, "data/sceneassets/crowd/crowd_scarf_0.rx3", priority)
			gr:CreateMaterial(crowd, lod, meshpart, "crowd_accs.fx")
			
			if (i < 2) then
				gr:AddAsset(crowd, lod, texpart, "data/sceneassets/flag/flag_${db.crowd.homeTeamID}.rx3", priority)
				gr:SetTexture(crowd, lod, meshpart, "textures", "diffuseTexture", texpart, "flag_${db.crowd.homeTeamID}_"..flagindex)
			else
				gr:AddAsset(crowd, lod, texpart, "data/sceneassets/flag/flag_${db.crowd.awayTeamID}.rx3", priority)
				gr:SetTexture(crowd, lod, meshpart, "textures", "diffuseTexture", texpart, "flag_${db.crowd.awayTeamID}_"..flagindex)
			end
			
			end
			
		end
	end

	-- banner textures
	-- names indexed as follows
	-- 0 - empty
	-- 1 - home
	-- 2 - away
	-- 3 - neutral
	gr:AddAsset(crowd, lod, "bannertex0", "data/sceneassets/banner/banner_0.rx3", priority)
	gr:AddAsset(crowd, lod, "bannertex1", "data/sceneassets/banner/banner_${db.crowd.homeTeamID}.rx3;data/sceneassets/banner/banner_0.rx3", priority)
	gr:AddAsset(crowd, lod, "bannertex2", "data/sceneassets/banner/banner_${db.crowd.awayTeamID}.rx3;data/sceneassets/banner/banner_0.rx3", priority)
	gr:AddAsset(crowd, lod, "bannertex3", "data/sceneassets/banner/banner_0.rx3", priority)
	
	

	return crowd
end


homecrowdsize = 4
awaycrowdsize = 4
neutralcrowdsize = 4

function setHomeCrowdSize(size)
homecrowdsize = size
end

function setAwayCrowdSize(size)
awaycrowdsize = size
end


seatsvisable = true

function removeSeats()
seatsvisable = false
end

--Revolution Mod V1.2
--Edited by scouser09
